<template>
  <div>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header>
        <div class="left">
          <div class="logo"></div>
          <span class="title">电商后台管理系统</span>
        </div>

        <el-button type="info" @click="logout">退出登录</el-button>
      </el-header>
      <!-- 主题区域 -->
      <el-container>
        <!-- 侧边栏菜单 -->
        <el-aside :width="iscollapse ? '70px' : '170px'">
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="iscollapse"
            :collapse-transition="false"
            :default-active="$route.path"
            router
          >
            <!-- 一级菜单 -->
            <el-submenu
              v-for="item in menuList"
              :key="item.id"
              :index="'' + item.order"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <i :class="iconsList[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                v-for="i in item.children"
                :key="i.id"
                :index="'/' + i.path"
              >
                <template>
                  <i class="el-icon-menu"></i>
                  <span>{{ i.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧主体区域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: {},
      iconsList: {
        '125': 'el-icon-user-solid',
        '103': 'el-icon-s-grid',
        '101': 'el-icon-shopping-bag-1',
        '102': 'el-icon-tickets',
        '145': 'el-icon-s-data',
      },
      iscollapse: false,
    };
  },
  methods: {
    //退出登录
    logout() {
      sessionStorage.clear("token");
      this.$router.push("/login");
      this.$message.warning("您已退出登录");
    },

    //获取导航栏
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      if (res.meta.status !== 200) {
        return this.$message.error("获取菜单失败");
      } else {
        this.menuList = res.data;
        // console.log(this.menuList)
      }
    },

    //切换导航栏展示与不展示
    toggleCollapse(){
      this.iscollapse = !this.iscollapse;
    }
  },
  created() {
    this.getMenuList();
  },
};
</script>

<style scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.left {
  display: flex;
  align-items: center;
}
.logo {
  width: 50px;
  height: 50px;
  background-image: url("../assets/logo.png");
  background-size: 50px 50px;
}
.title {
  color: #ffffff;
  font-size: 20px;
  margin-left: 10px;
}

.el-aside {
  background-color: #333744;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-menu{
  border: none;
}

.el-main {
  background-color: #eaedf1;
}
</style>